//
//  StartViewController.m
//  RefreshDemo
//
//  Created by UniHan_iMac on 17/4/11.
//  Copyright © 2017年 UniHan_iMac. All rights reserved.
//

#import "StartViewController.h"
#import "HomeViewController.h"

@interface StartViewController ()<UIScrollViewDelegate>
{
    float _scrollWidth;
    float _scrollHeight;
    int _currentPage;
    NSTimer *_timer;
}
@end

@implementation StartViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setStartPage];
    
    // 为_startPageControl的currentPage属性添加观测者
    [_startPageControl addObserver:self forKeyPath:@"currentPage" options:NSKeyValueObservingOptionNew context:@"currentPageChanged"];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.navigationController.navigationBarHidden = YES;
    self.edgesForExtendedLayout = UIRectEdgeNone;
}

#pragma mark 监听currentPage的值
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if(_startPageControl.currentPage == 2)
    {
        // performSelector延迟方法
//        [self performSelector:@selector(delaySelector) withObject:nil afterDelay:3.0];
        
        // GCD延迟方法
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            HomeViewController *homeVC = [[HomeViewController alloc] init];
            [self.navigationController pushViewController:homeVC animated:YES];
        });
    }
}
#pragma mark 设置启动图片
- (void)setStartPage
{
    _currentPage = 1;
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    _scrollWidth = screenSize.width;
    _scrollHeight = screenSize.height - 20;
    _startScrollView.contentSize = CGSizeMake(_scrollWidth * 3, _scrollHeight);
    for (int i = 0; i < 3; i++) {
        UIImageView *startImageView = [[UIImageView alloc] init];
        startImageView.frame = CGRectMake(_scrollWidth * i, 0, _scrollWidth, _scrollHeight);
        startImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i + 1]];
        [_startScrollView addSubview:startImageView];
    }
    [self.view bringSubviewToFront:_startPageControl];
    _startScrollView.delegate = self;
    // 开启定时器
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(scrollScreen:) userInfo:_startScrollView repeats:YES];
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:3.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
        if(_currentPage < 3)
        {
            _startScrollView.contentOffset = CGPointMake(_scrollWidth * _currentPage, 0);
            _startPageControl.currentPage = _currentPage;
            _currentPage++;
        }
    }];
}
- (void)scrollScreen:(NSTimer *)timer
{
    if(_currentPage > 2)
        return;
    UIScrollView *scrollView = timer.userInfo;
    scrollView.contentOffset = CGPointMake(_scrollWidth * _currentPage, 0);
    _currentPage++;
}

#pragma mark 开始减速时触发的方法
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    _currentPage = _startScrollView.contentOffset.x / _scrollWidth;
    _startPageControl.currentPage = _currentPage;
}

- (void)dealloc
{
    // 销毁定时器
    [_timer invalidate];
    _timer = nil;
}
@end
